package com.lsl.gulimall.product.service.impl;

import com.lsl.gulimall.product.entity.AttrEntity;
import com.lsl.gulimall.product.service.AttrService;
import com.lsl.gulimall.product.vo.request.BaseAttrsInVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsl.common.utils.PageUtils;
import com.lsl.common.utils.Query;

import com.lsl.gulimall.product.dao.ProductAttrValueDao;
import com.lsl.gulimall.product.entity.ProductAttrValueEntity;
import com.lsl.gulimall.product.service.ProductAttrValueService;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveBaseAttrs(Long id, List<BaseAttrsInVo> attrs) {
        List<ProductAttrValueEntity> productAttrValueEntities = attrs.stream().map(
                (attr) -> {
                    ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                    //通过attrId获取属性AttrEntity
                    AttrEntity attrEntity = attrService.getById(attr.getAttrId());
                    productAttrValueEntity.setSpuId(id);
                    productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                    productAttrValueEntity.setAttrId(attr.getAttrId());
                    productAttrValueEntity.setQuickShow(attr.getShowDesc());
                    productAttrValueEntity.setAttrValue(attr.getAttrValues());
                    return productAttrValueEntity;
                }
        ).collect(Collectors.toList());
        this.saveBatch(productAttrValueEntities);
    }
}